/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xmlasm.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import utils.SimpleJSonObject;
import utils.XMLUtils;
import xmlasm.bo.UserBO;
import xmlasm.dao.UserDAO;
import xmlasm.dto.User;
import xmlasm.sync.FilePathHolder;

/**
 *
 * @author Thuc
 */
public class EditUserAction extends ActionSupport {

    private int userId;
    private String username;
    private String fullname;
    private String email;
    private String currentEmail;
    private String password;
    private String newPassword;
    private String confirm;
    private boolean status;
    private InputStream inputStream;
    private String outputFO;

    public String updateAccount() throws Exception {
        SimpleJSonObject resultObject = new SimpleJSonObject();

        String userFilePath = FilePathHolder.getInstance().getUserXMLPath();
        Document doc = XMLUtils.parseDom(userFilePath);
        boolean checkEmail = UserBO.checkDuplicateEmail(email, doc);
        if (!email.equals(currentEmail) && checkEmail) {
            resultObject.put("message", "failureemail");
            resultObject.put("info", "Đây là email hiện đã có người khác sử dụng.");
        } else {
            Map session = ActionContext.getContext().getSession();
            User user = new User();
            user = (User) session.get("USER");
            user.setFullName(fullname);
            user.setEmail(email);
            UserBO.updateInfoToXML(user);
            boolean result = UserDAO.updateUser(user);
            if (result) {
                session.put("USER", user);
                resultObject.put("message", "success");
                resultObject.put("info", "ĐÃ LƯU THAY ĐỔI.");
            }
        }
        inputStream = new ByteArrayInputStream(resultObject.toString().getBytes("UTF-8"));
        return SUCCESS;
    }

    public String updatePassword() throws Exception {
        SimpleJSonObject resultObject = new SimpleJSonObject();

        if (UserDAO.checkLogin(username, password) == null) {
            resultObject.put("message", "failure");
            resultObject.put("info", "Password hiện tại không đúng.");
        } else {
            Map session = ActionContext.getContext().getSession();
            User user = new User();
            user = (User) session.get("USER");
            user.setPassword(newPassword);
            UserBO.updateInfoToXML(user);
            boolean result = UserDAO.updateUser(user);
            if (result) {
                session.put("USER", user);
                resultObject.put("message", "success");
                resultObject.put("info", "ĐÃ LƯU THAY ĐỔI.");
            }
        }
        inputStream = new ByteArrayInputStream(resultObject.toString().getBytes("UTF-8"));
        return SUCCESS;
    }

    public String setActivation() {
        if (UserDAO.setActivation(userId, status)) {
            UserBO.setActivation(userId, status);
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("notify", "CẬP NHẬT THÀNH CÔNG :x");
            return SUCCESS;
        }
        return ERROR;
    }

    public String setPermission() {
        if (UserDAO.setPermission(userId, status)) {
            UserBO.setPermission(userId, status);
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("notify", "CẬP NHẬT THÀNH CÔNG :x");
            return SUCCESS;
        }
        return ERROR;
    }

    public String validateToPDF() {
        Map<String, String> params = new HashMap<String, String>();
        params.put("feedXML", FilePathHolder.getInstance().getFeedXMLPath());
        String webPath = FilePathHolder.getInstance().getWebPath();
        String xslFilePath = webPath + "WEB-INF/xsl/joinUserAndFeed.xsl";
        String outputXMLRelativePath = "WEB-INF/dataXML/user-feed.xml";

        XMLUtils.doTransform(FilePathHolder.getInstance().getUserXMLPath(),
                xslFilePath, webPath + outputXMLRelativePath, params);
        try {
            XMLUtils.validateXML(webPath + outputXMLRelativePath, webPath + "WEB-INF/schema/user-feed.xsd");
        } catch (SAXException ex) {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setAttribute("ERROR", ex.getMessage());
            return ERROR;
        }
        return SUCCESS;
    }

    public String getUserPDF() {
        String webPath = FilePathHolder.getInstance().getWebPath();
        String inputXML = webPath + "WEB-INF/dataXML/user-feed.xml";
        String inputXSL = webPath + "WEB-INF/xsl/userFO.xsl";
        outputFO = webPath + "WEB-INF/pdf/fo/user.fo";
        XMLUtils.doTransform(inputXML, inputXSL, outputFO);
        return SUCCESS;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCurrentEmail() {
        return currentEmail;
    }

    public void setCurrentEmail(String currentEmail) {
        this.currentEmail = currentEmail;
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getOutputFO() {
        return outputFO;
    }

    public void setOutputFO(String outputFO) {
        this.outputFO = outputFO;
    }
}
